<%@ page import="com.cdvtc.flower.dao.OrderDao" %>
<%@ page import="com.cdvtc.flower.dao.impl.OrderDaoImpl" %>
<%@ page import="com.cdvtc.flower.model.Order" %>
<%@ page import="com.cdvtc.flower.dao.ProductsDao" %>
<%@ page import="com.cdvtc.flower.dao.impl.ProductsDaoImpl" %>
<%@ page import="com.cdvtc.flower.model.Products" %>
<%@ page import="java.util.List" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.cdvtc.flower.dao.CustomerDao" %>
<%@ page import="com.cdvtc.flower.dao.impl.CustomerDaoImpl" %>
<%@ page import="com.cdvtc.flower.model.Customer" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/5/30
  Time: 17:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>个人信息</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/main.css">
</head>
<body>
<jsp:include page="commons/header.jsp"></jsp:include>
    <div class="container">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title">
                    修改订单信息
                </h3>
            </div>
            <div class="panel-body" style="padding: 20px;">
                <form action="OrderModifyServlet" class="form-horizontal" role="form" method="get" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="order_id" class="col-sm-2 control-label">订单号：</label>
                        <%
                            Integer originalProductId = null;
                            Integer originalCustomerId = null;
                            String order_id = request.getParameter("order_id"); // 获取product_id字符串
                            OrderDao orderDao = new OrderDaoImpl();
                            Order order = orderDao.getOrderById(Integer.parseInt(order_id)); // 获取单个商品
                            pageContext.setAttribute("order", order); // 而不是整个products列表
                            originalProductId = order.getProduct_id();
                            pageContext.setAttribute("originalProductId", originalProductId);
                            originalCustomerId = order.getCustomer_id();
                            pageContext.setAttribute("originalCustomerId", originalCustomerId);
                        %>
                        <div class="col-sm-10">
                            <input type="text" readonly class="form-control" name="order_id" id="order_id" placeholder="" value="${order.order_id}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="product_id" class="col-sm-2 control-label">商品编号：</label>
                        <div class="col-sm-10">
                            <%
                                ProductsDao productsDao = new ProductsDaoImpl();
                                List<Products> productsList; // 更改变量名以避免混淆
                                productsList = productsDao.getAllProducts();
                                pageContext.setAttribute("productsList", productsList); // 使用新的变量名
                            %>
                            <select class="form-control" name="product_id" id="product_id" onchange="calculateTotalPrice()">
                                <option value="">请选择商品</option>
                                <c:forEach var="product" items="${productsList}" varStatus="status">
                                    <c:choose>
                                        <c:when test="${not empty originalProductId and originalProductId == product.product_id}">
                                            <!-- 如果当前产品的ID等于originalProductId，则设置为selected -->
                                            <option value="${product.product_id}" data-price="${product.sale_price}" selected="selected">${product.product_name}</option>
                                        </c:when>
                                        <c:otherwise>
                                            <!-- 否则，只是普通的option -->
                                            <option value="${product.product_id}" data-price="${product.sale_price}">${product.product_name}</option>
                                        </c:otherwise>
                                    </c:choose>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                    <label for="customer_id" class="col-sm-2 control-label">顾客ID：</label>
                    <div class="col-sm-10">
                        <%
                            CustomerDao customerDao = new CustomerDaoImpl();
                            List<Customer> customerList; // 更改变量名以避免混淆
                            customerList = customerDao.getAllCustomer();
                            pageContext.setAttribute("customerList", customerList); // 使用新的变量名
                        %>
                        <select class="form-control" name="customer_id" id="customer_id">
                            <option value="">请选择客户</option>
                            <c:forEach var="customer" items="${customerList}" varStatus="status">
                                <c:choose>
                                    <c:when test="${not empty originalCustomerId and originalCustomerId == customer.customer_id}">
                                        <!-- 如果当前category的ID等于originalCategoryId，则设置为selected -->
                                        <option value="${customer.customer_id}" selected="selected">${customer.user_name}</option>
                                    </c:when>
                                    <c:otherwise>
                                        <!-- 否则，只是普通的option -->
                                        <option value="${customer.customer_id}">${customer.user_name}</option>
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                    <div class="form-group">
                    <label for="quantity" class="col-sm-2 control-label">数量：</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="quantity" id="quantity" placeholder="请输入购买数量" value="${order.quantity}" oninput="calculateTotalPrice()">
                    </div>
                </div>
                    <div class="form-group">
                        <label for="order_date_time" class="col-sm-2 control-label">订单时间：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="order_date_time" id="order_date_time" placeholder="请选择订单时间"
                                   value="${order.order_date_time}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="total_price" class="col-sm-2 control-label">交易金额：</label>
                        <div class="col-sm-10">
                            <input type="text"  readonly class="form-control" name="total_price" id="total_price" placeholder=""  value="${order.total_price}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="review" class="col-sm-2 control-label">评论：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="review" id="review" placeholder="" value="${order.review}">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">确定</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-datepicker.min.js"></script>
<script src="js/bootstrap-datepicker.zh-CN.min.js"></script>
<script>
    $().ready(function () {
        // 设置日期选择器
        $('#order_date_time').datepicker({
            language: 'zh-CN', //语言
            clearBtn: true,//清除按钮
            format: "yyyy-mm-dd"//日期格式
        });
    });

    function calculateTotalPrice() {
        var quantity = parseFloat(document.getElementById('quantity').value) || 0; // 转换为浮点数并处理空值
        var productId = document.getElementById('product_id').value;
        var totalPriceElement = document.getElementById('total_price');
        var selectedOption = document.querySelector('#product_id option[value="' + productId + '"]');
        var price = selectedOption ? parseFloat(selectedOption.getAttribute('data-price')) || 0 : 0; // 从data-price属性中获取价格

        var totalPrice = quantity * price;
        totalPriceElement.value = totalPrice.toFixed(2); // 保留两位小数并设置到input中
    }
</script>
</body>
</html>


